package com.jzo2o.foundations.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class ServeServiceImpl implements IServeService {
    private final ServeMapper serveMapper;
    private final RegionMapper regionMapper;
    private final static int ON_SALE = 2;

    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO dto) {
        Long regionId = dto.getRegionId();
        Page<Serve> page = Page.of(dto.getPageNo(), dto.getPageSize());
        List<ServeResDTO> serveResDTOS = serveMapper.queryList(regionId);
        long total = page.getTotal();
        long pages = page.getPages();
        PageResult<ServeResDTO> pageResult = new PageResult<>();
        pageResult.setPages(pages);
        pageResult.setTotal(total);
        pageResult.setList(serveResDTOS);
        return pageResult;
    }

    @Override
    public void add(List<ServeUpsertReqDTO> dtoList) {
        if (CollUtil.isEmpty(dtoList)) {
            throw new RuntimeException("服务列表不能为空");
        }
        for (ServeUpsertReqDTO dto : dtoList) {
            Serve serve = new Serve();
            BeanUtils.copyProperties(dto, serve);
            Long regionId = dto.getRegionId();
            Region region = regionMapper.selectById(regionId);
            if (BeanUtils.isEmpty(region)) {
                throw new RuntimeException("区域不存在");
            }
            String cityCode = region.getCityCode();
            serve.setCityCode(cityCode);
            serveMapper.insert(serve);
        }
        log.info("添加服务-结束");
    }

    @Override
    public void update(Long id, BigDecimal price) {
        Serve serve = serveMapper.selectById(id);
        if(BeanUtils.isEmpty(serve)){
            throw new RuntimeException("服务不存在");
        }
        serve.setPrice(price);
        serveMapper.updateById(serve);
    }

    @Override
    public void onSale(Long id) {
        Serve serve = serveMapper.selectById(id);
        if(BeanUtils.isEmpty(serve)){
            throw new RuntimeException("服务不存在");
        }
        serve.setSaleStatus(ON_SALE);
        serveMapper.updateById(serve);
    }
}
